package com.voicechat.firstmet.client;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.voicechat.firstmet.utils.ConstantSet;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.client
 * @ClassName: OssConfig
 * @Description: oss配置类
 * @Author: ljp
 * @CreateDate: 2018/9/25 14:01
 * @Version: 1.0
 */
public class OssConfig {
    private static String TAG = "OssConfig";
    public static String ENDPOINT = "oss-cn-hangzhou.aliyuncs.com";
    public static String ACCESSID = "LTAIHNVm4MGhOcP5";
    public static String ACCESSKEY = "oTG6tLXmyeDZzMUbAtOEayppXs0Eaz";
    public static String BUCKET = "faint-app"; //
    public static String ALBUM = "album"; //动态帖子
    public static String CHATVOICE = "chat_voice"; //chat_voice 对话
    public static String HEAD = "head"; //head 头像 + 语音介绍+实名认证图片
    public static String VOICE = "voice"; //voice 语聊
    public static String GIF = "gift"; //礼物oss地址
    public static String OSS_HTTP = "https://faint-app.oss-cn-hangzhou.aliyuncs.com/"; //oss请求地址
    /**
     * @method
     * @description oss图片下载流转换的转换
     * @date: 2018/9/25 19:51
     * @author: ljp
     * @return
     */
    public static Bitmap readInputStreamToBitmap(InputStream ins) {
        if (ins == null) {
            return null;
        }
        byte[] b;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            byte[] buffer = new byte[1024];
            int size = -1;
            int len = 0;// 已经接收长度
            size = ins.read(buffer);
            while (size != -1) {
                len = len + size;//
                bos.write(buffer, 0, size);
                if (buffer.length == len) {// 接收完毕
                    break;
                }
                size = ins.read(buffer);
            }
            b = bos.toByteArray();
            bos.close();
           // Log.d(TAG, "readInputStreamToBitmap: --------"+b.length);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        if (b.length != 0) {
            return BitmapFactory.decodeByteArray(b, 0, b.length);
        }
        return null;
    }
    /**
     * @method
     * @description oss图片下载流转换的转换
     * @date: 2018/9/25 19:51
     * @author: ljp
     * @return
     */
    public static byte[] readInputStreamToByte(InputStream inputStream) throws IOException {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int num = inputStream.read(buffer);
            while (num != -1) {
                baos.write(buffer, 0, num);
                num = inputStream.read(buffer);
            }
            baos.flush();
            return baos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
        return null;

    }
    /**
     * 将InputStream写入本地文件
     * @param destination 写入本地目录
     * @param input 输入流
     * @throws IOException IOException
     */
    public static void writeToLocal(String destination, InputStream input,String path)
            throws IOException {
        File dirFile = new File(destination);
        if (!dirFile.exists()) {
            dirFile.mkdirs(); // 第一段
        }
        File file = new File(destination + ConstantSet.VOICEINTR_PATH+ path);
        if (!file.getParentFile().exists()) {
            // 分两次mkdirs，是为了避免目录层级过高导致目录创建失败的情况
            file.getParentFile().mkdirs();
        }
        if (!file.exists()) {
            file.createNewFile();
        }
        int index;
        byte[] bytes = new byte[1024];
        FileOutputStream downloadFile = new FileOutputStream(file);
        while ((index = input.read(bytes)) != -1) {
            downloadFile.write(bytes, 0, index);
            downloadFile.flush();
        }
        input.close();
        downloadFile.close();
    }
    /**
     * 将InputStream写入本地文件
     * @param destination 写入本地目录.下载礼物
     * @param input 输入流
     * @throws IOException IOException
     */
    public static void writeToLocalGif(String destination, InputStream input,String path)
            throws IOException {
        File dirFile = new File(destination);
        if (!dirFile.exists()) {
            dirFile.mkdirs(); // 第一段
        }
        File file = new File(destination + ConstantSet.GIF_PATH+ path);
        if (!file.getParentFile().exists()) {
            // 分两次mkdirs，是为了避免目录层级过高导致目录创建失败的情况
            file.getParentFile().mkdirs();
        }
        if (!file.exists()) {
            file.createNewFile();
        }
        int index;
        byte[] bytes = new byte[1024];
        FileOutputStream downloadFile = new FileOutputStream(file);
        while ((index = input.read(bytes)) != -1) {
            downloadFile.write(bytes, 0, index);
            downloadFile.flush();
        }
        input.close();
        downloadFile.close();
    }
    /**
     * 将InputStream写入本地文件
     * @param destination 写入本地目录.下载帖子语音
     * @param input 输入流
     * @throws IOException IOException
     */
    public static void writeToLocalPost(String destination, InputStream input,String path)
            throws IOException {
        File dirFile = new File(destination);
        if (!dirFile.exists()) {
            dirFile.mkdirs(); // 第一段
        }
        File file = new File(destination + ConstantSet.VOICE_POST+ path);
        if (!file.getParentFile().exists()) {
            // 分两次mkdirs，是为了避免目录层级过高导致目录创建失败的情况
            file.getParentFile().mkdirs();
        }
        if (!file.exists()) {
            file.createNewFile();
        }
        int index;
        byte[] bytes = new byte[1024];
        FileOutputStream downloadFile = new FileOutputStream(file);
        while ((index = input.read(bytes)) != -1) {
            downloadFile.write(bytes, 0, index);
            downloadFile.flush();
        }
        input.close();
        downloadFile.close();
    }

}
